﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace Poet.Converters
{
    public class BrushToStringConverter : IValueConverter
    {
        public static BrushConverter BrushConverter = new BrushConverter();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? null : BrushConverter.ConvertToInvariantString(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                var r = value == null || string.IsNullOrEmpty(value.ToString())
                            ? null
                            : BrushConverter.ConvertFromInvariantString(value.ToString());
                return r;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
